import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.filters.*;
import flash.geom.*;

private function bitmapDataGet( $key:String ):BitmapData{
	var temp:*;
	temp = tdata( $key );
	if( temp is BitmapData ){
		return temp.clone();
	}else{
		return null;
	}
}
private function bitmapAPG_( $original:BitmapData, $alpha:BitmapData ):BitmapData{
	var result:BitmapData, point:Point;
	result = bitmapData( $original.width, $original.height, 0x0 );
	result.merge( $original, $original.rect, POINT, 0x100, 0x100, 0x100, 0x100 );
	result.copyChannel( $alpha, $alpha.rect, POINT, BitmapDataChannel.RED, BitmapDataChannel.ALPHA );
	return result;
}
final public function bitmap( $bitmapData:BitmapData ):Bitmap{
	return new Bitmap( $bitmapData, 'auto', true );
}
final public function bitmapData( $width:uint = 0, $height:uint = 0, $background:uint = 0, $transparent:Boolean = true ):BitmapData{
	if( $width === 0 ){
		$width = STAGE.stageWidth;
	}
	if( $height === 0 ){
		$height = STAGE.stageHeight;
	}
	return new BitmapData( $width, $height, $transparent, $background );
}
final public function bitmapBlit( $blit:String ):BitmapData{
	tdata( $blit ).render();
	return tdata( $blit ).bitmapData.clone();
}
final public function bitmapDraw( $base:*, $copy:*, $x:Number = -2147483648, $y:Number = -2147483648, $width:Number = -2147483648, $height:Number = -2147483648, $color:Array = null, $blendMode:String = null ):BitmapData{
	var matrix:Matrix, color:ColorTransform;
	if( $copy is String ){
		$copy = tdata( $copy ).data;
	}
	if( $base is String ){
		$base = tdata( $base ).data.clone();
	}else if( $base === null ){
		$base = bitmapData( $copy.width, $copy.height );
	}
	if( $x === $NONE ){}else{
		if( matrix ){}else{
			matrix = new Matrix;
		}
		matrix.tx = $x;
	}
	if( $y === $NONE ){}else{
		if( matrix ){}else{
			matrix = new Matrix;
		}
		matrix.ty = $y;
	}
	if( $width === $NONE ){}else{
		if( matrix ){}else{
			matrix = new Matrix;
		}
		matrix.a = $width/$copy.width;
	}
	if( $height === $NONE ){}else{
		if( matrix ){}else{
			matrix = new Matrix;
		}
		matrix.d = $height/$copy.height;
	}
	if( $color ){
		color = new ColorTransform( $color[0], $color[1], $color[2], $color[3], $color[4], $color[5], $color[6], $color[7] );
	}
	$base.draw( $copy, matrix, 	color, $blendMode, null, true );
	return $base;
}
final public function bitmapCopy( $base:*, $copy:*, $x:Number = 0, $y:Number = 0, $isClone:Boolean = true ):BitmapData{
	if( $base is String ){
		$base = tdata( $base );
	}
	if( $isClone ){
		$base = $base.clone();
	}
	if( $copy is String ){
		$copy = tdata( $copy );
	}
	POINT.x = $x;
	POINT.y = $y;
	$base.copyPixels( $copy, $copy.rect, POINT, null, null, true );
	POINT.x = 0;
	POINT.y = 0;
	return $base;
}
final public function bitmapCopyTile( $base:*, $copy:*, $cellX:Number = 1, $cellY:Number = 1, $isClone:Boolean = true ):BitmapData{
	var i:int, j:int;
	if( $base is String ){
		$base = tdata( $base );
	}
	if( $isClone ){
		$base = $base.clone();
	}
	if( $copy is String ){
		$copy = tdata( $copy );
	}
	for( i = 0 ; i < $cellX ; ++i ){
		POINT.x = $copy.width * i;
		for( j = 0 ; j < $cellY ; ++j ){
			POINT.y = $copy.height * j;
			$base.copyPixels( $copy, $copy.rect, POINT, null, null, true );
		}
	}
	POINT.x = 0;
	POINT.y = 0;
	return $base;
}
final public function bitmapResize( $base:*, $width:uint = 0, $height:uint = 0, $isSimple:Boolean = true ):BitmapData{
	if( $base is String ){
		$base = tdata( $base ).data.clone();
	}
	$base = $base.clone();
	if( $width === 0 ){
		if( $height === 0 ){
			return $base;
		}
		$width = $base.width*$height/$base.height;
	}
	if( $height === 0 ){
		$height = $base.height*$width/$base.width;
	}
	
	POINT.y = POINT.x = 0;
	if( $isSimple ){
		if( resizerSimple ){}else{
			resizerSimple = new ShaderFilter( new Shader( new RESIZERSIMPLE ) );
		}
		resizerSimple.shader.data.scaleX.value = [$width/$base.width];
		resizerSimple.shader.data.scaleY.value = [$height/$base.height];
		$base.applyFilter( $base, $base.rect, POINT, resizerSimple );
	}else{
		if( resizer ){}else{
			resizer = new ShaderFilter( new Shader( new RESIZER ) );
		}
		resizer.shader.data.scaleX.value = [$width/$base.width];
		resizer.shader.data.scaleY.value = [$height/$base.height];
		$base.applyFilter( $base, $base.rect, POINT, resizer );
	}
	
	return bitmapCopy( bitmapData( $width, $height ), $base );
}

final public function bitmapTile( $cellX:int, $cellY:int, $bitmap:* ):Array{
	var temp:BitmapData, i:uint, x:uint, y:uint, result:Array;
	
	if( $bitmap is String ){
		temp = tdata( $bitmap ).data.clone();
	}else{
		temp = $bitmap;
	}
	
	RECT.width = temp.width/$cellX;
	RECT.height = temp.height/$cellY;
	
	result = [];
	i = 0;
	POINT.y = POINT.x = 0;
	for( y = 0 ; y < $cellY ; ++y ){
		for( x = 0 ; x < $cellX ; ++x ){
			RECT.x = RECT.width * x;
			RECT.y = RECT.height * y;
			result[i] = bitmapData( RECT.width, RECT.height );
			result[i].copyPixels( temp, RECT, POINT, null, null, true );
			++i;
		}
	}
	temp.dispose();
	return result;
}
final public function bitmapMap( $map:Object, $bitmap:* ):Object{
	var temp:BitmapData, key:*, result:Object, tempBitmap:BitmapData, i:int, j:int, tile:Array;
	if( $bitmap is String ){
		temp = tdata( $bitmap ).clone();
	}else{
		temp = $bitmap.clone();
	}
	result = {};
	POINT.y = POINT.x = 0;
	for( key in $map ){
		i = $map[key].length;
		if( i === 2 ){
			tile = bitmapTile( $map[key][0], $map[key][1], temp );
			for( i = 0, j = tile.length ; i < j ; ++i ){
				result[key + i.toString()] = tile[i];
			}
		}else{
			RECT.x = $map[key][0];
			RECT.y = $map[key][1];
			RECT.width = $map[key][2];
			RECT.height = $map[key][3];
			tempBitmap = bitmapData( RECT.width, RECT.height );
			tempBitmap.copyPixels( temp, RECT, POINT, null, null, true );
			if( i === 6 ){
				tile = bitmapTile( $map[key][4], $map[key][5], tempBitmap );
				for( i = 0, j = tile.length ; i < j ; ++i ){
					result[key + i.toString()] = tile[i];
				}
			}else{
				result[key] = tempBitmap;
			}
		}
	}
	temp.dispose();
	return result;
}

final public function bitmapAPG( $original:String, $alpha:String, $target:String = '', ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderAdd( null, function():void{
		var result:BitmapData;
		result = bitmapAPG_( tdata( $original ), tdata( $alpha ) );
		if( $target ){
			if( SELF._trees[$target] ){
				SELF._trees[$target].data = result;
				SELF._trees[$target + '@size'].data = [result.width, result.height];
			}else{
				addTree( '@DATA', $target, 'image', result, param );
				addTree( '@DATA', $target+'@size', 'data', [result.width, result.height], null );
			}
		}else{
			tdata( $original ).dispose();
			SELF._trees[$original].data = result;
		}
	} );
}
////알파 체크---------------------------------------------------------------------------------------------------------------------------------------
//private function checkAlphaBitmapdata( $bitmapData:BitmapData ):Boolean{
//	var temp:Vector.<Vector.<Number>>, totalPixel:uint, result:Boolean;
//	
//	temp = $bitmapData.histogram();
//	totalPixel = $bitmapData.width*$bitmapData.height;
//	
//	if( temp[3][255] === totalPixel ){ // 알파값이 없다.
//		result = false;
//	}else{ // 알파값이 있다.
//		result = true;
//	}
//	
//	return result;
//}
//알파 채널만 분리---------------------------------------------------------------------------------------------------------------------------------------  
//private function extractAlphaChannel( $bitmapData:BitmapData ):BitmapData{
//	var result:BitmapData;
//	result = new BitmapData( $bitmapData.width, $bitmapData.height, false );
//	result.copyChannel( $bitmapData, $bitmapData.rect, new Point( 0, 0 ), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );
//	return result;
//}
////알파와 RGB 채널 분리---------------------------------------------------------------------------------------------------------------------------------------
//private function extractAlphaAndRGB( $bitmapData:BitmapData ):Array{
//	var result:Array, alphaBitmapData:BitmapData, rgbBitmapData:BitmapData;
//	alphaBitmapData = new BitmapData( $bitmapData.width, $bitmapData.height, false );
//	rgbBitmapData = new BitmapData( $bitmapData.width, $bitmapData.height, false );
//	alphaBitmapData.copyChannel( $bitmapData, $bitmapData.rect, new Point( 0, 0 ), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );
//	rgbBitmapData.copyChannel( $bitmapData, $bitmapData.rect, new Point( 0, 0 ), BitmapDataChannel.RED, BitmapDataChannel.RED );
//	rgbBitmapData.copyChannel( $bitmapData, $bitmapData.rect, new Point( 0, 0 ), BitmapDataChannel.GREEN, BitmapDataChannel.GREEN );
//	rgbBitmapData.copyChannel( $bitmapData, $bitmapData.rect, new Point( 0, 0 ), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE );
//	
//	result = [alphaBitmapData, rgbBitmapData];
//	return result;
//}